package com.kwan.xframe.xbyte;


import com.blankj.utilcode.util.ConvertUtils;
import com.kwan.xframe.util.HexUtil;

public class XBinaryReader {

	//操作的数组
	public byte[] data;
	public int position;
	public XBinaryReader(byte[] data) {
		this.data = data;
	}


	public String str16(){

		int len = 2;
		//data
		byte[] tempData = ByteArrayUtil.subBytes(data, position, len);
		//Byte
		position += len;

		return bytesToHexString(tempData);

	}

	public String str32(){

		int len = 4;
		//data
		byte[] tempData = ByteArrayUtil.subBytes(data, position, len);
		//Byte
		position += len;



		return ConvertUtils.bytes2HexString(tempData);

	}



	public String str64(){

		int len = 8;
		//data
		byte[] tempData = ByteArrayUtil.subBytes(data, position, len);
		//Byte
		position += len;

		return bytesToHexString(tempData);

	}


	public static String bytesToHexString(byte[] src){
		StringBuilder stringBuilder = new StringBuilder("");
		if (src == null || src.length <= 0) {
			return null;
		}
		for (int i = 0; i < src.length; i++) {
			int v = src[i] & 0xFF;
			String hv = Integer.toHexString(v);
			if (hv.length() < 2) {
				stringBuilder.append(0);
			}
			stringBuilder.append(hv);
		}
		return stringBuilder.toString();
	}



	public int readInt16() {
		int len = 2;
		//data
		byte[] tempData = ByteArrayUtil.subBytes(data, position, len);
		//Byte
		position += len;
		return  HexUtil.byte2int(tempData);
	}

	public int readInt32() {
		int len = 4;
		//data
		byte[] tempData = ByteArrayUtil.subBytes(data, position, len);
		//Byte
		position += len;
		return HexUtil.byteArrayToInt(tempData,0);

		//return ByteArrayUtil.bytes2int(tempData);
	}

	public long readUInt32() {
		int len = 4;
		//data
		byte[] tempData = ByteArrayUtil.subBytes(data, position, len);
		//Byte
		position += len;
		return HexUtil.byteArrayToLong(tempData,0);
	}

	public long readUInt64() {
		int len = 8;
		//data
		byte[] tempData = ByteArrayUtil.subBytes(data, position, len);
		//Byte
		position += len;
		return HexUtil.byteArrayToLong64(tempData,0);
	}

	public byte readByte() {
		byte temp = data[position];
		position++;
		return temp;
	}

	public boolean readBool() {
		byte temp = data[position];
		position++;
		return temp == 1;
	}


	public String readStringEndWith0() {

		int strlen = -1;
		//int pos = (int)pack.Position;
		for (int i = position; i < data.length; i++) {
			if (data[i] == 0) {
				strlen = i - position;
				break;
			}
		}
		if (strlen == 0) {
			position++;
			return "";
		} else if (strlen > 0) {
			//	String str = Encoding.UTF8.GetString(buf, pos, strlen);
			String str = new String(ByteArrayUtil.subBytes(data, position, strlen));
			position += strlen + 1;
			return str;
		} else {
			return null;
		}
	}

	public String readStringEndWithx0() {

		int strlen = -1;
		//int pos = (int)pack.Position;
		for (int i = position; i < data.length; i++) {
			if (data[i] == '\0') {
				strlen = i - position;
				break;
			}
		}
		if (strlen == 0) {
			position++;
			return "";
		} else if (strlen > 0) {
			//	String str = Encoding.UTF8.GetString(buf, pos, strlen);
			String str = new String(ByteArrayUtil.subBytes(data, position, strlen));
			position += strlen + 1;
			return str;
		} else {
			return null;
		}
	}


}
